package com.javasm.aftersales;

import com.javasm.commons.base.BaseController;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.entity.TableDatas;
import com.javasm.commons.enums.E;
import com.javasm.commons.util.Constants;
import com.javasm.process.ProcessService;
import com.javasm.process.entity.ProcessTask;
import com.javasm.sys.CurrentLogin;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("saleReturn")

public class SaleReturnController extends BaseController {
    @Resource
    private SaleReturnService saleReturnService;

    @Resource
    private ProcessService processService;



    @GetMapping("{id}")
    public AxiosResult getByid(@PathVariable String id) {
        SaleReturn obj = this.saleReturnService.getById(id);
        return suc(obj);
    }
    
//    @GetMapping
//    public TableDatas list(SaleReturn obj) {
////        List<SaleReturn> list = this.saleReturnService.list(new QueryWrapper(obj));
//        List<SaleReturn> saleReturns = saleReturnService.listSaleReturnAndOrder(obj);
//        return toTableDatas(saleReturns);
//    }
    //查询
    @GetMapping
    public TableDatas list(SaleReturn obj) {
//        startPage();
//        List<SaleReturn> list = this.saleReturnService.list(new QueryWrapper(obj));
        List<SaleReturn> saleReturns = saleReturnService.listSaleReturnAndOrder(obj);
        return toTableDatas(saleReturns);
    }

    @PostMapping
    public AxiosResult add(@RequestBody SaleReturn saleReturn) {
        if (saleReturn.getCommodityList()==null|| saleReturn.getCommodityList().size()==0) return AxiosResult.error(E.NOT_SALES_RETURN_GOODS);
        //将状态设置为审核中
        saleReturn.setStatus(Constants.APPROVE_ING);
        boolean b = saleReturnService.saveAndSubmit(saleReturn);
//        boolean b = this.saleReturnService.save(saleReturn);
        return isok(b);
    }

    //审核
    @Transactional
    @PostMapping("/doApprove")
    public AxiosResult doApprove(@RequestBody ProcessTask processTasks){
        String applyId = processTasks.getApplyId();
        String approveContent = processTasks.getApproveContent();
        String approveResult = processTasks.getApproveResult();
        String hasNextNode = processService.approve(Constants.SALES_RETURN_PROCESS,processTasks, CurrentLogin.getLoginUserName());
        if (hasNextNode!=null){
            SaleReturn SaleReturn = new SaleReturn();
            SaleReturn.setSaleReturnId(applyId);
            SaleReturn.setStatus(approveResult);
            saleReturnService.updateById(SaleReturn);
        }
        return suc();
    }

    //审核跟踪
    @GetMapping("listTasks/{applyId}")
    public AxiosResult listTasks(@PathVariable String applyId ){
        List<ProcessTask> list = processService.getTaskByApplyOrderId(Constants.SALES_RETURN_PROCESS,applyId);
        return suc(list);
    }


    @PutMapping
    public AxiosResult updateById(@RequestBody SaleReturn saleReturn) {
        boolean b = this.saleReturnService.updateById(saleReturn);
        return isok(b);
    }
    
    @DeleteMapping("{ids}")
    public AxiosResult delByIds(@PathVariable String ids) {
        String[] idsArray = ids.split(",");
        boolean b = this.saleReturnService.removeByIds(Arrays.asList(idsArray));
        return isok(b);
    }
}

